
package example

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
    "gorm.io/gorm"
)

type VideoDataAudioService struct {}
// CreateVideoDataAudio 创建videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService) CreateVideoDataAudio(ctx context.Context, videoDataAudio *example.VideoDataAudio) (err error) {
	err = global.GVA_DB.Create(videoDataAudio).Error
	return err
}

// DeleteVideoDataAudio 删除videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService)DeleteVideoDataAudio(ctx context.Context, ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.VideoDataAudio{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&example.VideoDataAudio{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteVideoDataAudioByIds 批量删除videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService)DeleteVideoDataAudioByIds(ctx context.Context, IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.VideoDataAudio{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&example.VideoDataAudio{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateVideoDataAudio 更新videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService)UpdateVideoDataAudio(ctx context.Context, videoDataAudio example.VideoDataAudio) (err error) {
	err = global.GVA_DB.Model(&example.VideoDataAudio{}).Where("id = ?",videoDataAudio.ID).Updates(&videoDataAudio).Error
	return err
}

// GetVideoDataAudio 根据ID获取videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService)GetVideoDataAudio(ctx context.Context, ID string) (videoDataAudio example.VideoDataAudio, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&videoDataAudio).Error
	return
}
// GetVideoDataAudioInfoList 分页获取videoDataAudio表记录
// Author [yourname](https://github.com/yourname)
func (videoDataAudioService *VideoDataAudioService)GetVideoDataAudioInfoList(ctx context.Context, info exampleReq.VideoDataAudioSearch) (list []example.VideoDataAudio, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.VideoDataAudio{})
    var videoDataAudios []example.VideoDataAudio
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.VideoId != nil {
        db = db.Where("video_id = ?", *info.VideoId)
    }
    if info.AudioId != nil {
        db = db.Where("audio_id = ?", *info.AudioId)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&videoDataAudios).Error
	return  videoDataAudios, total, err
}
func (videoDataAudioService *VideoDataAudioService)GetVideoDataAudioPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
